/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.resource.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.JsonModel;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.resource.entity.*;
import com.lczyfz.istep.modules.resource.service.*;
import com.lczyfz.istep.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 培训方案Controller
 * @author dh
 * @version 2017-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/resource/resourceTrainprogram")
public class ResourceTrainprogramController extends BaseController {

	@Autowired
	private ResourceTrainprogramService resourceTrainprogramService;
	@Autowired
	private ResourceCoursesystemService resourceCoursesystemService;
	@Autowired
	private ResourceCtfSetService resourceCtfSetService;
	@Autowired
	private ResourcePaperService resourcePaperService;
	@Autowired
	private ResourceTrainprogramCoursesystemService resourceTrainprogramCoursesystemService;
	@Autowired
	private ResourceTrainprogramCtfsetService resourceTrainprogramCtfsetService;
	@Autowired
	private ResourceTrainprogramPaperService resourceTrainprogramPaperService;
	@ModelAttribute
	public ResourceTrainprogram get(@RequestParam(required=false) String id) {
		ResourceTrainprogram entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = resourceTrainprogramService.get(id);
		}
		if (entity == null){
			entity = new ResourceTrainprogram();
		}
		return entity;
	}
	@Autowired
	private SystemService systemService;
	@RequiresPermissions("resource:resourceTrainprogram:view")
	@RequestMapping(value = {"list", ""})
	public String list(ResourceTrainprogram resourceTrainprogram, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(systemService.isSuperAdmin())
		{
			model.addAttribute("isSuperAdmin", "true");
		}
		else
		{
			model.addAttribute("isSuperAdmin", "false");
		}
		Page<ResourceTrainprogram> page = resourceTrainprogramService.findPage(new Page<ResourceTrainprogram>(request, response), resourceTrainprogram);
		model.addAttribute("page", page);
		return "modules/resource/resourceTrainprogramList";
	}

	@RequiresPermissions("resource:resourceTrainprogram:view")
	@RequestMapping(value = "form")
	public String form(ResourceTrainprogram resourceTrainprogram, Model model, String type) {

		ResourceCoursesystem resourceCoursesystem = new ResourceCoursesystem();
		ResourceCtfSet resourceCtfSet = new ResourceCtfSet();
		ResourcePaper resourcePaper= new ResourcePaper();
		List<ResourceCoursesystem> resourceCoursesystemList = resourceCoursesystemService.findList(resourceCoursesystem);
		List<ResourceCtfSet> resourceCtfSetList = resourceCtfSetService.findList(resourceCtfSet);
		List<ResourcePaper> resourcePaperList = resourcePaperService.findList(resourcePaper);

		if (resourceTrainprogram != null && resourceTrainprogram.getId() != null && !resourceTrainprogram.getId().equals("")) {
			ResourceTrainprogramCoursesystem resourceTrainprogramCoursesystem=new ResourceTrainprogramCoursesystem();
			resourceTrainprogramCoursesystem.setTrainprogramId(resourceTrainprogram.getId());
			List<ResourceTrainprogramCoursesystem> temp=resourceTrainprogramCoursesystemService.findList(resourceTrainprogramCoursesystem);
			for (int i = 0; i < resourceCoursesystemList.size(); i++) {
				if (temp.size() == 0) {
					break;
				} else {
					for (int j = 0; j < temp.size(); j++) {
						if (resourceCoursesystemList.get(i).getId().equals(temp.get(j).getCoursesystemId())) {
							resourceCoursesystemList.get(i).setIsChoose("true");
							temp.remove(j);
							break;
						}
					}
				}

			}
			ResourceTrainprogramCtfset resourceTrainprogramCtfset=new ResourceTrainprogramCtfset();
			resourceTrainprogramCtfset.setTrainprogramId(resourceTrainprogram.getId());
			List<ResourceTrainprogramCtfset> temp1=resourceTrainprogramCtfsetService.findList(resourceTrainprogramCtfset);
			for (int i = 0; i < resourceCtfSetList.size(); i++) {
				if (temp1.size() == 0) {
					break;
				} else {
					for (int j = 0; j < temp1.size(); j++) {
						if (resourceCtfSetList.get(i).getId().equals(temp1.get(j).getCtfSetId())) {
							resourceCtfSetList.get(i).setIsChoose("true");
							temp1.remove(j);
							break;
						}
					}
				}

			}
			ResourceTrainprogramPaper resourceTrainprogramPaper=new ResourceTrainprogramPaper();
			resourceTrainprogramPaper.setTrainprogramId(resourceTrainprogram.getId());
			List<ResourceTrainprogramPaper> temp2=resourceTrainprogramPaperService.findList(resourceTrainprogramPaper);
			for (int i = 0; i < resourcePaperList.size(); i++) {
				if (temp2.size() == 0) {
					break;
				} else {
					for (int j = 0; j < temp2.size(); j++) {
						if (resourcePaperList.get(i).getId().equals(temp2.get(j).getPaperId())) {
							resourcePaperList.get(i).setIsChoose("true");
							temp2.remove(j);
							break;
						}
					}
				}

			}
		}

		if(type!=null&&type.equals("copy"))
		{
			resourceTrainprogram.setTrainprogramName(resourceTrainprogram.getTrainprogramName() + "-副本");
		}
		if(systemService.isSuperAdmin())
		{
			model.addAttribute("isSuperAdmin", "true");
		}else
			model.addAttribute("isSuperAdmin", "false");
		model.addAttribute("type", type);
		model.addAttribute("resourceCoursesystemList", resourceCoursesystemList);
		model.addAttribute("resourceCtfSetList", resourceCtfSetList);
		model.addAttribute("resourcePaperList", resourcePaperList);
		model.addAttribute("resourceTrainprogram", resourceTrainprogram);
		return "modules/resource/resourceTrainprogramForm";
	}

	@RequiresPermissions("resource:resourceTrainprogram:edit")
	@RequestMapping(value = "save")
	public String save(ResourceTrainprogram resourceTrainprogram, Model model, RedirectAttributes redirectAttributes,String coursesystemIds,String ctfSetIds,String paperIds, String type) {
		if (!beanValidator(model, resourceTrainprogram)){
			return form(resourceTrainprogram, model,type);
		}
		logger.debug("type: [{}]", type);
		logger.debug("coursesystemIds: [{}]", coursesystemIds);
		logger.debug("ctfSetIds: [{}]", ctfSetIds);
		logger.debug("paperIds: [{}]", paperIds);
		logger.debug("type: [{}]", type);
		if (type != null && type.equals("copy")) {
			logger.debug("复制培训方案！");
			ResourceTrainprogram resourceTrainprogram1 = new ResourceTrainprogram();
			resourceTrainprogram1.setTrainprogramName(resourceTrainprogram.getTrainprogramName());
			ResourceTrainprogram temp=resourceTrainprogramService.findTrainprogramByName(resourceTrainprogram1);
//			List<ResourceTrainprogram> temp = resourceTrainprogramService.findList(resourceTrainprogram1);
//			logger.debug("temp.size()===" + temp.size());

			if (temp != null && temp.getTrainprogramName().equals(resourceTrainprogram.getTrainprogramName())) {
				addMessage(redirectAttributes, "复制培训方案'" + resourceTrainprogram.getTrainprogramName() + "'失败，该课程体系已存在");
			} else {
				if (coursesystemIds == null || coursesystemIds.equals("")) {
					addMessage(redirectAttributes, "复制培训方案失败，请选择需要的课程体系");
				}
				else {
					ResourceTrainprogram trainprogram=new ResourceTrainprogram();
					trainprogram.setTrainprogramName(resourceTrainprogram.getTrainprogramName());
					trainprogram.setTrainprogramAttribute(resourceTrainprogram.getTrainprogramAttribute());
					trainprogram.setRemarks(resourceTrainprogram.getRemarks());

					resourceTrainprogramService.save(trainprogram);
					resourceTrainprogramCoursesystemService.save(coursesystemIds, trainprogram);
					resourceTrainprogramCtfsetService.save(ctfSetIds, trainprogram);
					resourceTrainprogramPaperService.save(paperIds, trainprogram);
					addMessage(redirectAttributes, "复制培训方案'" + resourceTrainprogram.getTrainprogramName() + "'成功");
				}
			}
		}else{
			if (resourceTrainprogramService.get(resourceTrainprogram) == null) {
				logger.debug("新增===");

				ResourceTrainprogram resourceTrainprogram1 = new ResourceTrainprogram();
				resourceTrainprogram1.setTrainprogramName(resourceTrainprogram.getTrainprogramName());
				ResourceTrainprogram temp=resourceTrainprogramService.findTrainprogramByName(resourceTrainprogram1);

//				List<ResourceTrainprogram> temp = resourceTrainprogramService.findList(resourceTrainprogram1);
//				logger.debug("temp.size()===" + temp.size());

				if (temp != null && temp.getTrainprogramName().equals(resourceTrainprogram.getTrainprogramName())) {
					addMessage(redirectAttributes, "新增培训方案'" + resourceTrainprogram.getTrainprogramName() + "'失败，该课程体系已存在");
				} else {
					if (coursesystemIds == null || coursesystemIds.equals("")) {
						addMessage(redirectAttributes, "新增培训方案失败，请选择需要的课程体系");
					}
					else {
						resourceTrainprogramService.save(resourceTrainprogram);
						resourceTrainprogramCoursesystemService.save(coursesystemIds, resourceTrainprogram);
						resourceTrainprogramCtfsetService.save(ctfSetIds, resourceTrainprogram);
						resourceTrainprogramPaperService.save(paperIds, resourceTrainprogram);
						addMessage(redirectAttributes, "新增培训方案'" + resourceTrainprogram.getTrainprogramName() + "'成功");
					}
				}
			} else {
				logger.debug("修改===");
				ResourceTrainprogram temp=resourceTrainprogramService.findTrainprogramByName(resourceTrainprogram);
				if(temp!=null&&!temp.getId().equals(resourceTrainprogram.getId()))
				{
					addMessage(redirectAttributes,"修改培训方案失败，该课程体系已存在");
					return "redirect:"+Global.getAdminPath()+"/resource/resourceTrainprogram/?repage";

				}


				if (coursesystemIds == null || coursesystemIds.equals("")) {
					addMessage(redirectAttributes, "修改培训方案失败，请选择需要的课程体系");
				}else {
					resourceTrainprogramService.save(resourceTrainprogram);
					resourceTrainprogramCoursesystemService.update(coursesystemIds, resourceTrainprogram);
					resourceTrainprogramCtfsetService.update(ctfSetIds, resourceTrainprogram);
					resourceTrainprogramPaperService.update(paperIds, resourceTrainprogram);
					addMessage(redirectAttributes, "修改培训方案'" + resourceTrainprogram.getTrainprogramName() + "'成功");
				}

			}
		}

//			resourceTrainprogramService.save(resourceTrainprogram);
//		addMessage(redirectAttributes, "保存培训方案成功");
		return "redirect:"+Global.getAdminPath()+"/resource/resourceTrainprogram/?repage";
	}
	
	@RequiresPermissions("resource:resourceTrainprogram:edit")
	@RequestMapping(value = "delete")
	public String delete(ResourceTrainprogram resourceTrainprogram, RedirectAttributes redirectAttributes) {
		resourceTrainprogramService.delete(resourceTrainprogram);
		addMessage(redirectAttributes, "删除培训方案成功");
		return "redirect:"+Global.getAdminPath()+"/resource/resourceTrainprogram/?repage";
	}

	//培训方案属性获得列表
	@ResponseBody
	@RequestMapping(value = "findListByAttribute")
	public JsonModel findListByAttribute(@RequestParam(required = false) String attribute) {
		JsonModel jsonModel = new JsonModel();
		Map<String,List> map = new HashMap<>();
		ResourceCoursesystem resourceCoursesystem=new ResourceCoursesystem();
		resourceCoursesystem.setCoursesystemAttribute(attribute);
		ResourceCtfSet resourceCtfSet=new ResourceCtfSet();
		resourceCtfSet.setCtfSetAttribute(attribute);
		ResourcePaper resourcePaper=new ResourcePaper();
		resourcePaper.setPaperAttribute(attribute);

		map.put("CourseSystemList",resourceCoursesystemService.findList(resourceCoursesystem));
		map.put("CtfSetList",resourceCtfSetService.findList(resourceCtfSet));
		map.put("PaperList", resourcePaperService.findList(resourcePaper));
		jsonModel.setDataObj(map);
		return jsonModel;
	}


	/**
	 * 查看培训方案详情
	 * @param resourceTrainprogram
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("resource:resourceCourse:view")
	@RequestMapping(value = "detail")
	public String detail(ResourceTrainprogram resourceTrainprogram, Model model, HttpServletRequest request, HttpServletResponse response) {
		logger.debug("resourceCoursesystem=== [{}]", resourceTrainprogram.getId());
		ResourceCoursesystem resourceCoursesystem=new ResourceCoursesystem();
		ResourceCtfSet resourceCtfSet = new ResourceCtfSet();
		ResourcePaper resourcePaper= new ResourcePaper();
		resourceCoursesystem.setTrainprogramId(resourceTrainprogram.getId());
		resourceCtfSet.setTrainprogramId(resourceTrainprogram.getId());
		resourcePaper.setTrainprogramId(resourceTrainprogram.getId());

		Page<ResourceCoursesystem> coursesystemPage = resourceCoursesystemService.findPageByTrainprogramId(new Page<ResourceCoursesystem>(request, response), resourceCoursesystem);
		Page<ResourceCtfSet> ctfSetPage = resourceCtfSetService.findPageByTrainprogramId(new Page<ResourceCtfSet>(request, response), resourceCtfSet);
		Page<ResourcePaper> paperPage = resourcePaperService.findPageByTrainprogramId(new Page<ResourcePaper>(request, response), resourcePaper);


		model.addAttribute("coursesystemPage", coursesystemPage);
		model.addAttribute("ctfSetPage", ctfSetPage);
		model.addAttribute("paperPage", paperPage);

		model.addAttribute("resourceTrainprogram", resourceTrainprogram);
		return "modules/resource/resourceTrainprogramDetail";
	}

}